package org.lanqiao.dao.impl;

import org.lanqiao.dao.TrainDao;
import org.lanqiao.entity.Train;
import org.lanqiao.util.DBUtil;

import java.util.List;

public class TrainDapImpl implements TrainDao {
    //添加
    @Override
    public int addTrain(Train train) {
        String sql="insert into train(account,password,name,address,birthday) values(?,?,?,?,?)";
        int row= DBUtil.executeUpdate(sql,train.getAccount(),train.getPassword(),train.getName(),train.getAddress(),train.getBirthday());
        return row;
    }
    //修改
    @Override
    public int updTrain(Train train) {
        String sql="update train set account=?,password=?,name=?,address=?,birthday=? where id=?";
        int row=DBUtil.executeUpdate(sql,train.getAccount(),train.getPassword(),train.getName(),train.getAddress(),train.getBirthday(),train.getId());
        return row;
    }
    //删除
    @Override
    public int delTrain(Integer id) {
        String sql="delete from train where id=?";
        int row=DBUtil.executeUpdate(sql,id);
        return row;
    }
    //根据id查询一个
    @Override
    public Train findByIdTrain(Integer id) {
        String sql="select * from train where id=?";
        List<Train> list=DBUtil.executeQuery(Train.class,sql,id);
        return list.isEmpty() ? null:list.get(0);
    }
    //查询所有
    @Override
    public List<Train> findAllTrain() {
        String sql="select * from train";
        return DBUtil.executeQuery(Train.class,sql);
    }
    //判断
    @Override
    public boolean isExistById(Integer id) {
        Train train=this.findByIdTrain(id);
        return train==null?false:true;
    }
}
